package com.note.jsoup;

import com.alibaba.fastjson.JSON;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * Created by XQ on 2017/6/5.
 */
public class GetWeatherInfoTool {
    public String getWeatherInfo(String cityName) throws IOException {

        String url = "http://weather.sina.com.cn/"+cityName;

//        发送get请求
        Document document = Jsoup.connect(url).get();

//        获取城市信息
        Element ctHeader = document.select("div.slider_i div.slider_ct div.slider_ct_header h4").first();
        Element ctDate = document.select("div.slider_i div.slider_ct p.slider_ct_date").first();
        Element ctTime= document.select("div.slider_i div.slider_ct div.slider_ct_time").first();
//        获取天气信息
        Element statesImg = document.select("div.slider_states p.slider_whicon_w img").first();
        Element degree = document.select("div.slider_states div.slider_degree").first();
        Element detail = document.select("div.slider_states p.slider_detail").first();

//        获取天气警告
        Element warnInfo = document.select("div.slider_warn div.slider_warn_i_tt p").first();
        Element warnVal = document.select("div.slider_warn div.slider_warn_i_c p.slider_warn_val").first();
//        获取穿衣信息
        Element cy = document.select("div.blk5_i p").get(0);
        Element sport = document.select("div.blk5_i p").get(1);
        WeatherInfo weatherInfo = new WeatherInfo(ctHeader.text(),ctDate.text(),ctTime.text(),statesImg.attr("src")
                ,degree.text(),detail.text(),warnInfo.text(),warnVal.text(),cy.text(),sport.text());
       WeatherInfo weatherInfo1 = new WeatherInfo(weatherInfo);
        return JSON.toJSONString(weatherInfo1);
    }
}
